package com.sm.ui;

import com.sm.domain.Course;
import com.sm.domain.Teacher;
import com.sm.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@Component
public class TeacherMenuUI extends JFrame {

    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StudentUI studentUI;
    @Autowired
    private CourseUI courseUI;
    @Autowired
    private ScoreUI scoreUI;
    @Autowired
    private ReportUI reportUI;
    // 声明界面组件
    private JTabbedPane tablePane;
    private JPanel p;
    private CardLayout card;
    // 创建工具栏
    JToolBar toolBar = new JToolBar();

    // 构造方法
    public TeacherMenuUI(){
        // 调用父类的构造方法
        super("管理员界面");
        // 设置主面板为CardLayout卡片布局
        card = new CardLayout();
        p = new JPanel();
        p.setLayout(card);
        tablePane = new JTabbedPane(JTabbedPane.TOP);
        p.add(tablePane, "tablePane");

        // 将主面板添加到窗体中
        getContentPane().add(p, BorderLayout.CENTER);
        // 设置窗体初始可见
        // 设置窗体初始最大化
        this.setSize(900, 600);
        // 设置窗口初始化居中
        this.setLocationRelativeTo(null);
        // 设置默认的关闭按钮操作为退出程序
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void createWelcome(){
        card.show(p, "tablePane");
        tablePane.removeAll();
        JPanel panel = new JPanel();
        String no = redisTemplate.opsForValue().get("teaNo")==null?"":redisTemplate.opsForValue().get("teaNo")+"";
        JLabel w = new JLabel("你好老师("+no+"),欢迎使用成绩发布系统");
        w.setFont(new java.awt.Font("幼圆", 1, 24));
        panel.add(w);
        tablePane.addTab("欢迎", panel);
    }

    // 初始化工具栏的方法
    public void init(){
        createWelcome();
        // 将工具栏添加到窗体北部（上面）
        getContentPane().add(toolBar, BorderLayout.NORTH);
        createMenu("学生信息管理", studentUI);
        createMenu("课程信息管理", courseUI);
        createMenu("成绩信息管理", scoreUI);
        createMenu("统计分析", reportUI);
        JButton btnMatchLog = new JButton("修改密码");
        // 注册监听
        btnMatchLog.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                String m = JOptionPane.showInputDialog("请输入新密码");
                if (m == null || m.equals("")) {
                    JOptionPane.showMessageDialog(null, "密码不能为空！", "信息提示",JOptionPane.WARNING_MESSAGE);
                    return;
                }
                Integer id = new Integer(redisTemplate.opsForValue().get("teacherId")+"");
                Teacher t = new Teacher();
                t.setId(id);
                t.setPassword(m);
                teacherService.updateById(t);
                JOptionPane.showMessageDialog(null, "修改成功", "修改数据信息提示",JOptionPane.INFORMATION_MESSAGE);
            }
        });
        toolBar.add(btnMatchLog);

    }

    private void createMenu(String menuName, BaseUI ui){

        JButton btnMatchLog = new JButton(menuName);
        // 注册监听
        btnMatchLog.addActionListener(e -> toView(ui));
        toolBar.add(btnMatchLog);
    }


    private void toView(BaseUI view){
        card.show(p, "tablePane");
        tablePane.removeAll();
        view.init(tablePane);
    }
}
